<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line('edit'). ' ' .$this->lang->line('booking');?></h3>
<script src="<?php echo base_url().'static/templates/backend/js/jquery.maskedinput.js';?>"></script>
<style>
    .name_num_cls{
        width:20%;
    }
    .fone_num_cls{
        width:10%;
    }
     input[type="radio"],button.btn.top{
        vertical-align: top;
    }
</style>

<?php if (isset($flash_message)  && $flash_message!=NULL ): ?>
<div class="alert alert-success"><a class="close" listcustomers-dismiss="alert">x</a><strong><?php echo $flash_message; ?></strong></div>
<?php endif; ?>
<div class="well">
    <?php echo form_open();?>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php 
                //mã KH
                echo form_label($this->lang->line('customerID'), 'customerID');
                echo form_input(array('name' => 'customerID', 'id' => 'customerID', 'value' => $bookingStatus->customerID,'readonly' => 'true'));
                echo form_error('customerID');
                //Hãng
                echo form_label($this->lang->line('airline'), 'airline');
                echo form_input(array('name' => 'airline', 'id' => 'airline', 'value' => $bookingStatus->airline,'readonly' => 'true'));
                echo form_error('airline');
                 //Mã đặt chỗ
                echo form_label($this->lang->line('booking_place_code'), 'booking_place_code');
                echo form_input(array('name' => 'booking_place_code', 'id' => 'booking_place_code', 'value' => $bookingStatus->booking_place_code,'readonly' => 'true'));
                echo form_error('booking_place_code');
                //Loại booking
                echo form_label($this->lang->line('booking_types'), 'booking_types');
                echo form_dropdown('booking_types', $this->bookingStatus_model->getListBookingTypes(), $bookingStatus->booking_types,'id="booking_types"');//disabled
                echo form_error('booking_types');
                //Hành trình
                echo '<div id="booking-msg" style="display:block">';
                echo form_label($this->lang->line('customersName'), 'customersName');
                echo form_textarea(array('name' => 'customersName', 'id' => 'customersName', 'value' => str_replace("||", "\n", $bookingStatus->booking_customer_name),'rows'=>'5','placeholder'=>$this->lang->line('customersName')));
                echo form_error('customersName');
                //Em bé
                echo form_label($this->lang->line('child'), 'child');
                echo form_input(array('name' => 'child', 'id' => 'child', 'value' => $bookingStatus->booking_child,'placeholder'=>$this->lang->line('child_placeholder')));
                echo form_error('child');
                
                foreach(json_decode($bookingStatus->booking) as $key => $values){
                        $info = explode("-", $values);    
                        echo '<div id="fone_addon'.$key.'" class="fonediv">';
                        //NGAY BAY
                        echo form_input(array('id' => 'datefly'.$key, 'value' => $info[0],'class' => 'fone_num_cls','placeholder'=>$this->lang->line('datefly')));
                        echo form_error('datefly');
                        echo ' ';
                        //số hiệu
                        echo form_input(array('id' => 'numberfly'.$key, 'value' => $info[1],'class' => 'fone_num_cls','placeholder'=>$this->lang->line('numberfly')));
                        echo form_error('numberfly');
                        echo ' ';
                        //Giờ bay
                        if (strlen(trim($info[2])) <5){
                            $info[2] ='0'.trim($info[2]);
                        }
                        
                        if (strlen(trim($info[4])) <5){
                            $info[4] ='0'.trim($info[4]);
                        }

                        echo form_input(array('id' => 'timefly'.$key, 'value' => trim($info[2]),'class' => 'fone_num_cls','placeholder'=>$this->lang->line('timefly')));
                        echo form_error('timefly');
                        echo ' ';
                        //Sb đi
                        echo form_input(array('id' => 'gosb'.$key, 'value' => trim($info[3]),'maxlength'=>'3','class' => 'fone_num_cls','placeholder'=>$this->lang->line('gosb')));
                        echo form_error('gosb');
                        echo ' ';
                        //Giờ đến
                        echo form_input(array('id' => 'timeat'.$key, 'value' => trim($info[4]),'class' => 'fone_num_cls','placeholder'=>$this->lang->line('timeat')));
                        echo form_error('timeat');
                        echo ' ';
                        //Sb đến
                        echo form_input(array('id' => 'stopsb'.$key, 'value' => trim($info[5]),'maxlength'=>'3','class' => 'fone_num_cls','placeholder'=>$this->lang->line('stopsb')));
                        echo form_error('stopsb');
                        echo ' ';
                        //booking-info
                        echo form_input(array('type' => 'hidden', 'name' => 'booking-info[]', 'id' => 'booking-info-'.$key, 'value' => ''));
                        echo '</div>';
                        ?>
                        <script>
                            $.mask.definitions['a'] = "[A-Z]";
                            $.mask.definitions['b'] = "[0-9]";
                            $.mask.definitions['*'] = "[A-Z0-9]";
                            
                            $("#numberfly<?php echo $key;?>").mask("******",{placeholder:" ",autoclear:false});//SO HIEU
                            $("#timefly<?php echo $key;?>").mask("99:99");//GIỜ BAY
                            $("#gosb<?php echo $key;?>").mask("aaa");//SB ĐI
                            $("#timeat<?php echo $key;?>").mask("99:99");//GIỜ ĐẾN
                            $("#stopsb<?php echo $key;?>").mask("aaa");//SB ĐẾN

                           $(function() {
                                    var $datepicker = $("#datefly<?php echo $key;?>");
                                    $datepicker.datepicker({ 
                                        dateFormat: 'dd/mm/yy',
                                        onClose: function() {
                                                  document.getElementById("booking-info-<?php echo $key;?>").value = document.getElementById("datefly<?php echo $key;?>").value 
                                      + '-' + document.getElementById("numberfly<?php echo $key;?>").value.trim()
                                      + '-' + document.getElementById("timefly<?php echo $key;?>").value
                                      + '-' + document.getElementById("gosb<?php echo $key;?>").value
                                      + '-' + document.getElementById("timeat<?php echo $key;?>").value
                                      + '-' + document.getElementById("stopsb<?php echo $key;?>").value;
                                        }
                                    });
                            });

                            $("#datefly<?php echo $key;?>").on('blur',function() {
                                document.getElementById("booking-info-<?php echo $key;?>").value = document.getElementById("datefly<?php echo $key;?>").value 
                                + '-' + document.getElementById("numberfly<?php echo $key;?>").value.trim()
                                + '-' + document.getElementById("timefly<?php echo $key;?>").value
                                + '-' + document.getElementById("gosb<?php echo $key;?>").value
                                + '-' + document.getElementById("timeat<?php echo $key;?>").value
                                + '-' + document.getElementById("stopsb<?php echo $key;?>").value;
                            });

                            $("#numberfly<?php echo $key;?>").on('blur',function() {
                                document.getElementById("booking-info-<?php echo $key;?>").value = document.getElementById("datefly<?php echo $key;?>").value 
                                + '-' + document.getElementById("numberfly<?php echo $key;?>").value.trim()
                                + '-' + document.getElementById("timefly<?php echo $key;?>").value
                                + '-' + document.getElementById("gosb<?php echo $key;?>").value
                                + '-' + document.getElementById("timeat<?php echo $key;?>").value
                                + '-' + document.getElementById("stopsb<?php echo $key;?>").value;
                            });

                            $("#gosb<?php echo $key;?>").on('blur',function() {
                                document.getElementById("booking-info-<?php echo $key;?>").value = document.getElementById("datefly<?php echo $key;?>").value 
                                + '-' + document.getElementById("numberfly<?php echo $key;?>").value.trim()
                                + '-' + document.getElementById("timefly<?php echo $key;?>").value
                                + '-' + document.getElementById("gosb<?php echo $key;?>").value
                                + '-' + document.getElementById("timeat<?php echo $key;?>").value
                                + '-' + document.getElementById("stopsb<?php echo $key;?>").value;
                            });

                            $("#stopsb<?php echo $key;?>").on('blur',function() {
                                document.getElementById("booking-info-<?php echo $key;?>").value = document.getElementById("datefly<?php echo $key;?>").value 
                                + '-' + document.getElementById("numberfly<?php echo $key;?>").value.trim()
                                + '-' + document.getElementById("timefly<?php echo $key;?>").value
                                + '-' + document.getElementById("gosb<?php echo $key;?>").value
                                + '-' + document.getElementById("timeat<?php echo $key;?>").value
                                + '-' + document.getElementById("stopsb<?php echo $key;?>").value;
                            });

                            $("#timefly<?php echo $key;?>").on('blur',function() {
                                var timesx=this.value;
                                var times = timesx.split(":");
                                if (timesx.indexOf('_',1) >0)
                                {
                                    alert("Thời gian nhập không đúng");
                                    this.value='';
                                    $(this).focus();
                                }else if (times[0] > 23 || times[1]>59)
                                {
                                    alert("Thời gian nhập không đúng");
                                    this.value='';
                                    $(this).focus();
                                } else {
                                    document.getElementById("booking-info-<?php echo $key;?>").value = document.getElementById("datefly<?php echo $key;?>").value 
                                        + '-' + document.getElementById("numberfly<?php echo $key;?>").value.trim()
                                        + '-' + document.getElementById("timefly<?php echo $key;?>").value
                                        + '-' + document.getElementById("gosb<?php echo $key;?>").value
                                        + '-' + document.getElementById("timeat<?php echo $key;?>").value
                                        + '-' + document.getElementById("stopsb<?php echo $key;?>").value;
                                }
                            });

                            $("#timeat<?php echo $key;?>").on('blur',function() {
                                var timesx=this.value;
                                var times = timesx.split(":");
                                if (timesx.indexOf('_',1) >0)
                                {
                                    alert("Thời gian nhập không đúng");
                                    this.value='';
                                    $(this).focus();
                                }else if (times[0] > 23 || times[1]>59)
                                {
                                    alert("Thời gian nhập không đúng");
                                    this.value='';
                                    $(this).focus();
                                } else {
                                    document.getElementById("booking-info-<?php echo $key;?>").value = document.getElementById("datefly<?php echo $key;?>").value 
                                        + '-' + document.getElementById("numberfly<?php echo $key;?>").value.trim()
                                        + '-' + document.getElementById("timefly<?php echo $key;?>").value
                                        + '-' + document.getElementById("gosb<?php echo $key;?>").value
                                        + '-' + document.getElementById("timeat<?php echo $key;?>").value
                                        + '-' + document.getElementById("stopsb<?php echo $key;?>").value;
                                }
                            });

                            document.getElementById("booking-info-<?php echo $key;?>").value = document.getElementById("datefly<?php echo $key;?>").value 
                                + '-' + document.getElementById("numberfly<?php echo $key;?>").value.trim()
                                + '-' + document.getElementById("timefly<?php echo $key;?>").value
                                + '-' + document.getElementById("gosb<?php echo $key;?>").value
                                + '-' + document.getElementById("timeat<?php echo $key;?>").value
                                + '-' + document.getElementById("stopsb<?php echo $key;?>").value;
                        </script>
                        <?php
                }
                echo '</div>';
                
                //total_pax
                echo form_label($this->lang->line('total_pax'), 'total_pax');
                echo form_input(array('name' => 'total_pax', 'id' => 'total_pax', 'value' => '','required'=>'required'));
                echo form_error('total_pax');
                //Giá net
                echo form_label($this->lang->line('net_fare').' <span class="required">*</span>', 'net_fare');
                echo form_input(array('name' => 'net_fare', 'id' => 'net_fare', 'value' => $bookingStatus->net_fare,'required'=>'required'));
                echo form_error('net_fare');
                echo form_input(array('type' => 'hidden','name' => 'net_fare_fake', 'id' => 'net_fare_fake', 'value' => $bookingStatus->net_fare));
                //Giá bán
                echo form_label($this->lang->line('sale_fare').' <span class="required">*</span>', 'sale_fare');
                echo form_input(array('name' => 'sale_fare', 'id' => 'sale_fare', 'value' => $bookingStatus->sale_fare,'required'=>'required'));
                echo form_error('sale_fare');
                echo form_input(array('type' => 'hidden','name' => 'sale_fare_fake', 'id' => 'sale_fare_fake', 'value' => $bookingStatus->sale_fare));
                //mã KH
                echo form_label($this->lang->line('note'), 'note');
                echo form_textarea(array('name' => 'note', 'id' => 'note', 'value' => $bookingStatus->note,'rows'=>5));
                echo form_error('note');
                
                echo form_input(array('type'=>'hidden','name' => 'voucher_id', 'id' => 'voucher_id', 'value' => $bookingStatus->voucher_id));
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('save'),'class' => 'btn btn-primary')); ?>
                <a href="<?php echo site_url('auth/bookingStatus'); ?>" class="btn"><?php echo $this->lang->line('cancel')?></a>
        </div>
    <?php echo form_close();?>
    <span id="simple-msg-submit"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>
<?php $this->load->view('templates/backend/lib'); ?>
<script src="<?php echo base_url().'static/templates/backend/js/autoNumeric.js';?>"></script>
<script>
$(document).ready(function() {
        var customerID = '<?php echo $bookingStatus->customerID;?>';
        $('form').submit(function() {
               var net_fare = $("#net_fare").val();
               var sale_fare= $("#sale_fare").val();
               if(net_fare == "" || sale_fare == ""){
                   alert("Vui lòng điền thông tin giá bán ,giá mua!");return false;
               }

               $("#simple-msg-submit").html("Vui lòng chờ... <img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/> ");
               setTimeout(function () {
                   $("#simple-msg-submit").hide();
               },2000);
               $(this).find("button[type='submit']").prop('disabled',true);
       });
       
       $("#customersName").on('blur',function() {
                totalpx();
       });
       
       $('#net_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
        $("#net_fare").blur(function() {
            document.getElementById("net_fare_fake").value = UnFormatNumber($('#net_fare').val());
        });
        $('#sale_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
        $("#sale_fare").blur(function() {
            document.getElementById("sale_fare_fake").value = UnFormatNumber($('#sale_fare').val());
        });
        $('#sale_fare').autoNumeric('init',{aSign:' VNĐ',mDec:0, pSign:'s' });
        $("#sale_fare").blur(function() {
            document.getElementById("sale_fare_fake").value = UnFormatNumber($('#sale_fare').val());
        });

        $("#sale_fare").blur(function() {
            var tien = ($('#sale_fare').autoNumeric('get')-$('#net_fare').autoNumeric('get'));
            $('#profit').autoNumeric('set',tien);
        });

        $("#child").keyup(function() {
            $(this).val($(this).val().replace(/([a-z])/,function(s){return s.toUpperCase()}));
        });
});

function totalpx(){
    var temx='';
        var xtem = $.trim($('#customersName').val().toUpperCase());
        xtem = xtem.replace(/[^a-zA-Z-\r\n-\n\r-\r-\n-\s]/g,"");
        xtem = xtem.replace(/\r\n|\n\r|\r|\n/g,"-");

        var arr_name = xtem.split('-');
        for(i=0;i<arr_name.length;i++)
        {
                var xx=$.trim(arr_name[i]);
                if (xx=='')
                {
                        arr_name.splice(i,1);
                        i=i-1;
                }	
        }	
        xtem = arr_name.join('-');
        $('#total_pax').val(arr_name.length);
}
function FormatNumber(x) {
    if (typeof x === "undefined") {
        return '';
    } else {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' VNĐ';
    }
};
///,|VNĐ|\s/g
function UnFormatNumber(x) {
    if (typeof x === "undefined") {
        return '';
    } else {
    return x.toString().replace(/,|VNĐ|\s/g, "");
    }
};
totalpx();
</script>
